package com.rh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rh.entity.EduTeacher;
import com.rh.entity.vo.EduTeacherPageVo;
import com.rh.mapper.EduTeacherMapper;
import com.rh.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rh.result.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
@Slf4j
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public ResultVo findTeacherPage(Long current, Long limit) {
        Page<EduTeacher>page=new Page<>(current,limit);
        baseMapper.selectPage(page,null);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        Map<String,Object>map=new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return ResultVo.ok().data(map);
    }
    /**
     * 条件查询带分页
     */
    @Override
    public ResultVo pageQuery(Long current, Long limit, EduTeacherPageVo eduTeacherVo) {
        //构造分页对象
        Page<EduTeacher>pageParam=new Page<>(current,limit);
        //构造条件构造器
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //如果eduTeacherVo直接返回空
        if(eduTeacherVo==null){
            //调用单分页方法
            return this.findTeacherPage(current,limit);
        }
        String name = eduTeacherVo.getName();
        Integer level = eduTeacherVo.getLevel();
        String begin = eduTeacherVo.getBegin();
        String end = eduTeacherVo.getEnd();
        //做校验
        if(!StringUtils.isEmpty(name)){
            //如果name不为空，构建条件模糊
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            //如果level不为空，构建条件头衔相等
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            //开始时间要大于等于数据库gmt_create字段
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            //结束时间小于等于数据库gmt_create字段
            wrapper.le("gmt_create",end);
        }
        //根据添加时间进行排序
        wrapper.orderByDesc("gmt_create");
        //调用Mapper里面的方法
        baseMapper.selectPage(pageParam,wrapper);
        //总数
        long total = pageParam.getTotal();
        //总数据
        List<EduTeacher> records = pageParam.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return ResultVo.ok().data(map);
    }
    /**
     * 添加教师
     */
    @Override
    public ResultVo addTeacher(EduTeacher eduTeacher) {
        //参数校验
        String name = eduTeacher.getName();
        String intro = eduTeacher.getIntro();
        String career = eduTeacher.getCareer();
        Integer level = eduTeacher.getLevel();
        String avatar = eduTeacher.getAvatar();
        Integer sort = eduTeacher.getSort();

        if (StringUtils.isEmpty(name)){
            return ResultVo.error().message("姓名不能为空");
        }
        if (StringUtils.isEmpty(intro)){
            return ResultVo.error().message("简介不能为空");
        }
        if(StringUtils.isEmpty(career)){
            return ResultVo.error().message("资历不能为空");
        }
        if(level!=1&&level!=2){
            return ResultVo.error().message("输入的头衔不存在");
        }
       /* if(StringUtils.isEmpty(avatar)){
            return ResultVo.error().message("请上传头像");
        }*/
        int insert = baseMapper.insert(eduTeacher);
        if (insert>0){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 查看教师数据
     * 添加到缓存key名 banner::selectIndexTeacherList
     * @return
     */
    @Cacheable(value = "banner",key = "'selectIndexTeacherList'")
    @Override
    public List<EduTeacher> teacherList() {
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();
        eduTeacherQueryWrapper.orderByDesc("id");
        eduTeacherQueryWrapper.last("limit 4");
        List<EduTeacher> eduTeacherList =baseMapper.selectList(eduTeacherQueryWrapper);
        return eduTeacherList;
    }

    /**
     * todo 重点-前台教师分页查询(底层的分页)
     */
    @Override
    public Map<String, Object> getTeacherList(Page<EduTeacher> pages) {
        //构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //根据id降序排列
        wrapper.orderByDesc("id");
        //调用方法分页
        baseMapper.selectPage(pages, wrapper);

        //创建一个Map集合封装分页数据
        Map<String,Object>map=new HashMap<>();

        //数据的list集合
        List<EduTeacher> records = pages.getRecords();
        //当前页
        long current = pages.getCurrent();
        //总页数
        long pages1 = pages.getPages();
        //每页记录数
        long size = pages.getSize();
        //总记录数
        long total = pages.getTotal();

        //是否有上一页
        boolean hasPrevious = pages.hasPrevious();
        //是否有下一页
        boolean hasNext = pages.hasNext();

        //封装参数
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages1);
        map.put("size",size);
        map.put("total",total);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);

        return map;
    }
}
